package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.FormTeamSetService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formteamset.dto.FormTeamSetDetailDTO;
import com.xbongbong.pro.formteamset.dto.FormTeamSetLinkFormEnableDTO;
import com.xbongbong.pro.formteamset.dto.FormTeamSetLinkFormListDTO;
import com.xbongbong.pro.formteamset.dto.FormTeamSetSaveDTO;
import com.xbongbong.pro.formteamset.vo.FormTeamSetDetailVO;
import com.xbongbong.pro.formteamset.vo.FormTeamSetLinkFormEnableVO;
import com.xbongbong.pro.formteamset.vo.FormTeamSetLinkFormListVO;
import com.xbongbong.pro.formteamset.vo.FormTeamSetSaveVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 *
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/teamFormSet")
public class FormTeamSetController {

    @Resource
    private FormTeamSetService formTeamSetService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * Description: 表单设置-团队成员详情
     * @param formTeamSetDetailDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/17 09:42
     * @since
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detail(@RequestBody @Valid FormTeamSetDetailDTO formTeamSetDetailDTO, BindingResult br) throws Exception {
        XbbResponse<FormTeamSetDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormTeamSetDetailVO formTeamSetDetailVO = formTeamSetService.detail(formTeamSetDetailDTO);
                response = new XbbResponse<>(formTeamSetDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 表单设置-团队成员设置保存
     * @param formTeamSetSaveDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/17 09:42
     * @since
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid FormTeamSetSaveDTO formTeamSetSaveDTO, BindingResult br) throws Exception {
        XbbResponse<FormTeamSetSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(formTeamSetSaveDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(formTeamSetSaveDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }

                FormTeamSetSaveVO formTeamSetSaveVO = formTeamSetService.save(formTeamSetSaveDTO);
                response = new XbbResponse<>(formTeamSetSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 选择关联列表列表
     * @param formTeamSetLinkFormListDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/17 09:42
     * @since
     */
    @RequestMapping(value = "/getLinkFormList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String linkFormList(@RequestBody @Valid FormTeamSetLinkFormListDTO formTeamSetLinkFormListDTO, BindingResult br) throws Exception {
        XbbResponse<FormTeamSetLinkFormListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormTeamSetLinkFormListVO formTeamSetLinkFormListVO = formTeamSetService.linkFormList(formTeamSetLinkFormListDTO);
                response = new XbbResponse<>(formTeamSetLinkFormListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     *
     * @Description: 是否展示关联列表
     * @param formTeamSetLinkFormEnableDTO
     * @param br
     * @return: java.lang.String
     * @Author: shen.yang
     * @Date: 2022/5/12  10:42
    */
    @RequestMapping(value = "/linkFormEnable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String linkFormEnable(@RequestBody @Valid FormTeamSetLinkFormEnableDTO formTeamSetLinkFormEnableDTO, BindingResult br) throws Exception {
        XbbResponse<FormTeamSetLinkFormEnableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormTeamSetLinkFormEnableVO formTeamSetLinkFormEnableVO = formTeamSetService.linkFormEnable(formTeamSetLinkFormEnableDTO);
                response = new XbbResponse<>(formTeamSetLinkFormEnableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

}
